package com.yanqu.road.dao.impl.activity.operamask;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.operamask.OperaMaskSceneRank;
import com.yanqu.road.entity.activity.operamask.game.OperaMaskSkill;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OperaMaskSceneRankDaoImpl extends TempDao {

    public static String getOperaMaskSceneRankSql = "select * from `t_c_opera_mask_scene_rank` where `activityId`=?";

    public Map<Integer, Map<Integer, LinkedList<OperaMaskSceneRank>>> getOperaMaskSceneRankMap(int activityId) {
        Map<Integer, Map<Integer, LinkedList<OperaMaskSceneRank>>> typeSceneListMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getOperaMaskSceneRankSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                OperaMaskSceneRank parse = parse(rs);
                typeSceneListMap.putIfAbsent(parse.getType(), new ConcurrentHashMap<>());
                typeSceneListMap.get(parse.getType()).putIfAbsent(parse.getSceneId(), new LinkedList<>());
                typeSceneListMap.get(parse.getType()).get(parse.getSceneId()).add(parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return typeSceneListMap;
    }

    public static String insertOperaMaskSceneRankSql = "replace into `t_c_opera_mask_scene_rank`(activityId,type,sceneId,rank,userId,score,useStep,skillList,time,singLevel,skillValue) values (?,?,?,?,?,?,?,?,?,?,?)";

    public boolean replaceData(LinkedList<OperaMaskSceneRank> rankList) {
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        int rank = 0;
        for (OperaMaskSceneRank operaMaskSceneRank : rankList) {
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            para.put(i++, new DbParameter(Types.INTEGER, operaMaskSceneRank.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, operaMaskSceneRank.getType()));
            para.put(i++, new DbParameter(Types.INTEGER, operaMaskSceneRank.getSceneId()));
            para.put(i++, new DbParameter(Types.INTEGER, ++rank));
            para.put(i++, new DbParameter(Types.BIGINT, operaMaskSceneRank.getUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, operaMaskSceneRank.getScore()));
            para.put(i++, new DbParameter(Types.INTEGER, operaMaskSceneRank.getUseStep()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(operaMaskSceneRank.getSkillList())));
            para.put(i++, new DbParameter(Types.BIGINT, operaMaskSceneRank.getTime()));
            para.put(i++, new DbParameter(Types.INTEGER, operaMaskSceneRank.getSingLevel()));
            para.put(i++, new DbParameter(Types.INTEGER, operaMaskSceneRank.getSkillValue()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(insertOperaMaskSceneRankSql, mapList) > 0) {
            return true;
        }
        return false;
    }

    protected OperaMaskSceneRank parse(ResultSet rs) throws SQLException {
        OperaMaskSceneRank operaMaskSceneRank = new OperaMaskSceneRank();
        operaMaskSceneRank.setActivityId(rs.getInt("activityId"));
        operaMaskSceneRank.setType(rs.getInt("type"));
        operaMaskSceneRank.setSceneId(rs.getInt("sceneId"));
        operaMaskSceneRank.setUserId(rs.getLong("userId"));
        operaMaskSceneRank.setScore(rs.getLong("score"));
        operaMaskSceneRank.setUseStep(rs.getInt("useStep"));
        operaMaskSceneRank.setSkillList(JSON.parseObject(rs.getString("skillList"), new TypeReference<List<OperaMaskSkill>>() {
        }));
        operaMaskSceneRank.setTime(rs.getLong("time"));
        operaMaskSceneRank.setSingLevel(rs.getInt("singLevel"));
        operaMaskSceneRank.setSkillValue(rs.getInt("skillValue"));
        return operaMaskSceneRank;
    }

}
